import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'package:flutter_application_cyber/net/cn_net_helper.dart';
import 'package:flutter_util_code/toast_utils.dart';
import 'package:flutter_application_cyber/model/cn_userInfo_model.dart';
import 'package:flutter_application_cyber/model/cn_anchor_model.dart';
import 'package:flutter_application_cyber/model/cn_follow_model.dart';
import 'package:flutter_application_cyber/tools/cn_log_pv_manger.dart';
import 'package:flutter_application_cyber/tools/cn_database_helper.dart';

///主播详情数据ViewModel

class CnAnchorViewModel extends ChangeNotifier {
  static CnAnchorViewModel? _info;

  CnAnchorViewModel._();

  factory CnAnchorViewModel.getInfo() {
    return _info ??= CnAnchorViewModel._();
  }

  ///******************************************************举报一级弹窗、举报二级弹窗相关业务逻辑************************************************************
  ///设置关注用户、取消关注   返回参数一：1取消关注  2关注用户 参数二：bool  true成功  false失败

  ///显示更多弹窗  function 当前 用于更多弹窗选择了关注、取消关注时回调
  showMoreDialog(BuildContext context, UserInfoModel? mUserDetailsInfoModel,
      Function function) {
    return showModalBottomSheet<int>(
      backgroundColor: Colors.white,
      context: context,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(15))),
      builder: (BuildContext context) {
        return SizedBox(
          height: 200,
          child: Column(
            children: [
              InkWell(
                //关注、取消关注
                onTap: () {
                  bool  isFollowed =  mUserDetailsInfoModel?.isFollow  ?? false ;
                  follow(mUserDetailsInfoModel?.userId ?? '', isFollowed,
                      (bool value) {
                        function(isFollowed ? 'followed':'follow');
                    if (value) {
                      ToastUtils.show(isFollowed ? 'followed successfully':'follow successfully');
                    } else {
                      ToastUtils.show(isFollowed ? 'followed failure':'follow failure');
                    }
                    Navigator.pop(context);
                  });
                  
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    (mUserDetailsInfoModel?.isFollow  ?? false ) ? 'Followed':'Follow',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                //屏蔽、取消屏蔽
                onTap: () {
                  if (mUserDetailsInfoModel?.isBlock ?? false) {
                    CnAnchorViewModel.getInfo().postRemoveRecord(
                        mUserDetailsInfoModel?.userId ?? "", (bool status) {
                      if (status) {
                        mUserDetailsInfoModel?.isBlock = false;
                        ToastUtils.show('Unblock successfully');
                      
                      } else {
                        ToastUtils.show('Unblock failure');
                      }
                      Navigator.pop(context);
                    });
                  } else {
                    CnAnchorViewModel.getInfo().postInsertRecord(
                        mUserDetailsInfoModel?.userId ?? "", 'Block', "",
                        (bool status) {
                      if (status) {
                        //删除数据库中这一条数据
                        CnDatabaseHelper.getInstance()?.deleteUser(mUserDetailsInfoModel?.userId ?? '');
                        mUserDetailsInfoModel?.isBlock = true;
                        ToastUtils.show('Block successfully');
                          //取消关注接口
                          CnAnchorViewModel.getInfo().follow(mUserDetailsInfoModel?.userId ?? "", true, (bool value){
                           });
                        CnLogPvManger.stat(
                            page: 'block',
                            subPage: 'subPage',
                            terPage: 'terPage',
                            broadcasterId: 'broadcasterId');
                      } else {
                        ToastUtils.show('Block failure');
                      }
                      function('block');
                      Navigator.pop(context);
                    });
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Block',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                onTap: () {
                  CnLogPvManger.stat(
                      page: 'report',
                      subPage: 'subPage',
                      terPage: 'terPage',
                      broadcasterId: 'broadcasterId');

                  Navigator.pop(context);
                  _showReportDialog(context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Report',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Container(
                color: Colors.black45,
                height: 5,
              ),
              InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Cancel',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  ///显示举报弹窗
  _showReportDialog(
      BuildContext context, UserInfoModel? mUserDetailsInfoModel) {
    return showModalBottomSheet<int>(
      backgroundColor: Colors.white,
      context: context,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(15))),
      builder: (BuildContext context) {
        return SizedBox(
          height: 285,
          child: Column(
            children: [
              InkWell(
                onTap: () {
                  submitReportInfo(
                      'Porngraphic', context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Porngraphic',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                onTap: () {
                  submitReportInfo(
                      'Falsegender', context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Falsegender',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                onTap: () {
                  submitReportInfo('Fraud', context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Fraud',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                onTap: () {
                  submitReportInfo(
                      'Politicl sensitive', context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'political sensitive',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Divider(height: 0.33, color: Colors.black45),
              InkWell(
                onTap: () {
                  submitReportInfo('Other', context, mUserDetailsInfoModel);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'other',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Container(
                color: Colors.black45,
                height: 5,
              ),
              InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(0, 12, 0, 12),
                  child: Text(
                    'Cancel',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  //解除屏蔽主播
    void postRemoveRecord(String userId, Function callback) async {
    CnNetHelper.postRemoveRecord(
        userId, (HDHResponseData configData, bool result, String msg) {
          callback(result);
        });
  }


  //屏蔽主播、举报主播
  void postInsertRecord(String userId, String? blockingType, String? reasonType,
      Function callback) async {
    try {
      HDHResponseData? response =
          await CnNetHelper.postInsertRecord(userId, blockingType, reasonType);
      if (null != response &&
          response.success == true &&
          response.code == 0 &&
          response.data != null) {
        if (response.data is bool) {
          callback(response.data);
        } else {
          callback(false);
        }
      } else {
        callback(false);
      }
    } catch (e) {
      callback(false);
    }
  }

  ///提交举报用户信息
  submitReportInfo(String? reasonType, BuildContext context,
      UserInfoModel? mUserDetailsInfoModel) {
    CnAnchorViewModel.getInfo().postInsertRecord(
        mUserDetailsInfoModel?.userId ?? "", 'Report', reasonType,
        (bool status) {
      if (status) {
        ToastUtils.show('Report successfully');
      } else {
        ToastUtils.show('Report failure');
      }
      Navigator.pop(context);
    });
  }

  //关注与取消关注
  void follow(String userId, bool isFollow, Function callBack) async {
    HDHResponseData? hdhResponseData;
    if (isFollow) {
      hdhResponseData = await CnNetHelper.postUnFriend(userId);
    } else {
      hdhResponseData = await CnNetHelper.postAddFriend(userId);
    }
    if (hdhResponseData != null &&
        hdhResponseData.code == 0 &&
        hdhResponseData.data != null) {
      callBack(hdhResponseData.data);
    } else {
      callBack(false);
    }
  }

  Future<List<CnAnchorModel>> getAnchorWallList({required int page}) async {
    HDHResponseData? response = await CnNetHelper.anchorWallGet(page: page);
    if (response != null && response.code == 0 && response.data != null) {
      print(response);
      return (response.data as List<dynamic>)
          .map((data) => CnAnchorModel.coverFromJson(data))
          .toList();
    } else {
      return <CnAnchorModel>[];
    }
  }

  //分页获取关注列表
  void getFollowPagingList(int limit, int page, Function callBack) async {
    try {
      HDHResponseData? data = await CnNetHelper.getFollowPagingList(limit, page);
      if (data != null && data.code == 0 && data.data != null) {
        List<CnFollowModel> followList = (data.data as List<dynamic>)
            .map((data) => CnFollowModel.coverFromJson(data))
            .toList();
        callBack(followList);
      } else {
        callBack([]);
      }
    } catch (e) {
      callBack([]);
    }
  }
}
